import router from './index.js'
import { useMenuStore } from "@/stores/menu.js";
import Layout from '@/layout/index.vue'


const whiteRouter = ['/', '/login', '/error', '/404']

router.beforeEach((to, from, next) => {

    const menuStore = useMenuStore()

    if (whiteRouter.indexOf(to.path) === -1) {
        if (menuStore.routerList.length === 0) {
            menuStore.generateRouter().then(() => {
                const routerList = menuStore.routerList
                router.addRoute({
                    component: Layout,
                    path: "/",
                    redirect: "index",
                    children: routerList
                })
                next({...to, replace: true})
            })
        } else {
            if (to.matched.length !== 0) {
                next()
            } else {
                next('/404')
            }
        }
    } else {
        next()
    }
})